package org.sixtynine.stock.controller.front;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.sixtynine.stock.Constants;
import org.sixtynine.stock.controller.AbstractFrontController;
import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.entity.CustomerUpgrade;
import org.sixtynine.stock.entity.PackageData;
import org.sixtynine.stock.entity.PaymentConfig;
import org.sixtynine.stock.entity.User;
import org.sixtynine.stock.service.CustomerUpgradeService;
import org.sixtynine.stock.service.GenericService;
import org.sixtynine.stock.service.IpgReturnService;
import org.sixtynine.stock.service.PackageDataService;
import org.sixtynine.stock.service.StockManagerService;
import org.sixtynine.stock.service.UserService;
import org.sixtynine.stock.service.business.StockUtil;
import org.sixtynine.stock.service.content.DataManagerService;
import org.sixtynine.stock.util.DateUtil;
import org.sixtynine.stock.util.MailManager;
import org.sixtynine.stock.util.SessionBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilan
 * 
 */
@Controller
public class MyAccountController extends AbstractFrontController {

	@Autowired
	private StockManagerService service;

	@Autowired
	private DataManagerService dataManagerService;

	@Autowired
	IpgReturnService ipgReturnService;

	@Autowired
	CustomerUpgradeService customerUpgradeService;

	@Autowired
	PackageDataService packageDataService;

	@Autowired
	UserService userService;

	@Autowired
	private GenericService genericService;

	@Autowired
	ApplicationContext context;
	
	//@Autowired
	//MailManager mailManager;

	@RequestMapping(value = "/myaccount/main")
	public ModelAndView showResults(HttpServletRequest request) {

		ModelAndView modelAndView = new ModelAndView("/myaccount/main");
		SessionBean sessionBean = SessionBean.getInstance(request);
		if (sessionBean.getUser() == null) {
			modelAndView = new ModelAndView("/login/login");
			modelAndView.addObject("registedUser", new User());
			return modelAndView;
		}

		showBannar(modelAndView);
		User logedUser = sessionBean.getUser();
		modelAndView.addObject("logedUser", logedUser);
		modelAndView.addObject("userLogged", true);
		modelAndView.addObject("isBroker", true);

		String message = request.getParameter("message");
		modelAndView.addObject("message", message);

		return modelAndView;
	}

	@RequestMapping(value = "/myaccount/upgrade")
	public ModelAndView addIpgReturn(HttpServletRequest request) {
		ModelAndView modelAndView = null;

		if (!isUserLogged(request)) {
			modelAndView = new ModelAndView("/login/login");
			modelAndView.addObject("registedUser", new User());
			return modelAndView;
		}

		modelAndView = new ModelAndView("/myaccount/upgrade");
		List<BaseEntity> packageList = genericService
				.findAll(PackageData.class);
		showBannar(modelAndView);
		modelAndView.addObject("packageList", packageList);
		modelAndView.addObject("userLogged", true);

		return modelAndView;
	}

	@RequestMapping(value = "/myaccount/sendinvitation", method = RequestMethod.POST)
	@ResponseBody
	public boolean sendInvitation(
			
			HttpServletRequest request) {
		ModelAndView modelAndView = null;

		//mailManager.sendMail("jtsolutionpvt@gmail.com",
			//"thilan87189@gmail.com", "Test Subject", "Test Message");

		return true;
	}

	@RequestMapping(value = "/myaccount/confirm")
	public ModelAndView addconfirm(HttpServletRequest request,
			@RequestParam(value = "id", required = false) Integer id) {
		ModelAndView modelAndView = null;
		SessionBean sessionBean = SessionBean.getInstance(request);
		User logedUser = sessionBean.getUser();

		PaymentConfig configData = (PaymentConfig) genericService.findById(1,
				PaymentConfig.class);
		PackageData packageConfrmData = (PackageData) genericService.findById(
				id, PackageData.class);

		if (!isUserLogged(request)) {
			modelAndView = new ModelAndView("/login/login");
			modelAndView.addObject("registedUser", new User());
			return modelAndView;
		}

		String orderId = null;

		String passWord = configData.getHashCode();
		String acqId = configData.getInstanceId();
		String mercId = configData.getMerchantId();
		UUID uuId = UUID.randomUUID();
		orderId = uuId.toString();
		String price = Float
				.toString((packageConfrmData.getPrice() * 10000 * 1000000));

		String sha1 = StockUtil.getEncryptedCode(passWord, mercId, acqId,
				orderId, price, Constants.IPG_CURRENCY_CODE);
		CustomerUpgrade customerUpgrade = new CustomerUpgrade();

		customerUpgrade.setIpgStatus(Constants.IPG_PROCESSING);
		customerUpgrade.setUser(logedUser);
		customerUpgrade.setOrderId(orderId);
		customerUpgrade.setPackg(packageConfrmData);
		customerUpgrade.setUpgradeDate(DateUtil.getNextPaidDate(
				packageConfrmData.getDuration(), new Date()));
		customerUpgrade.setUpgradeDec("update to premium");

		customerUpgradeService.saveCustomerUpgrade(customerUpgrade);

		modelAndView = new ModelAndView("/myaccount/confirm");

		modelAndView.addObject("registedUser", logedUser);
		modelAndView.addObject("configData", configData);
		modelAndView.addObject("hash", sha1);
		modelAndView.addObject("packageData", packageConfrmData);
		modelAndView.addObject("orderId", orderId);
		modelAndView.addObject("userLogged", true);

		showBannar(modelAndView);

		return modelAndView;
	}

}
